package mysql

import (
	"gorm.io/gorm"
	"week2-project/global"
)

type Video struct {
	gorm.Model
	VideoTitle   string `gorm:"type:varchar(30);comment:视频标题"`
	SubTitle     string `gorm:"type:varchar(40);comment:视频副标题"`
	VideoCover   string `gorm:"type:varchar(256);comment:视频封面"`
	VideoChannel string `gorm:"type:varchar(256);comment:视频频道"`
	VideoArea    string `gorm:"type:varchar(256);comment:视频地区"`
	IsRecommend  int8   `gorm:"type:tinyint(1);comment:是否推荐 1是 2否"`
}

func (v *Video) List(keyword string, page, pageSize int) ([]*Video, error) {
	var list []*Video
	err = global.Db.Where("video_title like ?", "%"+keyword+"%").Scopes(Paginate(page, pageSize)).Find(&list).Error
	return list, err
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

//db.Scopes(Paginate(r)).Find(&users)
//db.Scopes(Paginate(r)).Find(&articles)
